/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_SQLITE_DBSPI_H
#define GENERIK_SQLITE_DBSPI_H

/** \file sqlite.h
This file implements functionality for use in sqlite SPI code.
*/

#include "../database/sqlite.h"
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1

#ifdef SQLITE_DBSPI

#define SQLITE_DEFINE_FUNC(FUNC) \
	void FUNC(sqlite3_context* ctxt, int argc, sqlite3_value** argv)

#define SQLITE_END_FUNC

#define SQLITE_GET_DBSPI \
	generik::sqlite_dbspi _sqlite_dbspi_(sqlite3_context_db_handle(ctxt))
//	generik::dbspi& _dbspi_handle_ = _sqlite_dbspi_

#define SQLITE_CHECK_ARGC(VALUE) \
	if (argc != (VALUE)) return -1
	
#define SQLITE_GETARG_INT(INDEX) \
	sqlite3_value_int(argv[(INDEX)])

#define SQLITE_RETURN_INT(VALUE) \
	sqlite3_result_int(ctxt, (VALUE))

#define SQLITE_CREATE_FUNCTION(NAME, ARGC) \
	sqlite3_create_function(db, "dbspi_"#NAME, ARGC, SQLITE_ANY, 0, &dbspi_##NAME, 0, 0)

#define DBSPI_DEFINE_FUNC(FUNC) SQLITE_DEFINE_FUNC(FUNC)
#define DBSPI_END_FUNC SQLITE_END_FUNC
#define DBSPI_GET_HANDLE SQLITE_GET_DBSPI
#define DBSPI_HANDLE &_sqlite_dbspi_
#define DBSPI_GETARG_INT(INDEX) SQLITE_GETARG_INT(INDEX)
#define DBSPI_RETURN_INT(VALUE) SQLITE_RETURN_INT(VALUE)

#define DBSPI_MESSAGE_OUTPUT(WHAT, SOURCE, MESSAGE) std::clog<<#WHAT<<": ["<<SOURCE<<"] "<<MESSAGE<<std::endl;

#define DBSPI_LOG_MESSAGE_FROM_SOURCE(SOURCE, MESSAGE) DBSPI_MESSAGE_OUTPUT(INFO, SOURCE, MESSAGE)
#define DBSPI_LOG(MESSAGE) DBSPI_LOG_MESSAGE_FROM_SOURCE(__FUNCTION__, MESSAGE)

#define DBSPI_ERROR_MESSAGE_FROM_SOURCE(SOURCE, MESSAGE) DBSPI_MESSAGE_OUTPUT(ERROR, SOURCE, MESSAGE)
#define DBSPI_ERROR(MESSAGE) DBSPI_ERROR_MESSAGE_FROM_SOURCE(__FUNCTION__, MESSAGE)

#define DBSPI_TRACER DBSPI_LOG("[running]")

#endif // SQLITE_DBSPI

namespace generik
{

	/** Implements SPI functionality for the sqlite database backend.
	This class enables a backend function to make queries of its own, using SPI calls.
	Of course, in the sqlite case this is exactly the same as making regular queries!
	*/
	struct sqlite_dbspi
	: dbspi
	, sqlite
	{
		sqlite_dbspi(sqlite3* db_in)
		: sqlite(db_in)
		{
			GENERIK_TRACER;
		}
		
		~sqlite_dbspi()
		{
			GENERIK_TRACER;
		}
		
		inline void runquery(const std::string& cmd, callback cb, void* object)
		{
			sqlite::runquery(cmd, cb, object);
		}
	};
	
}

void register_functions(sqlite3* db);

extern "C"
{

	int sqlite3_extension_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi)
	{
		SQLITE_EXTENSION_INIT2(pApi);
		register_functions(db);
		return 0;
	}

}

#endif










